package com.itheima.advice;

import com.itheima.domain.Option;
import com.itheima.service.OptionService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class RecordInfoAdvice {

    @Autowired
    private OptionService service;
    public Object around(ProceedingJoinPoint point){
        try {
            Option option = new Option();
        //获取创建的时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String thisTime = now.format(formatter);
        option.setOpTime(thisTime);

        //获取类名和方法名
        String simpleName = point.getTarget().getClass().getSimpleName();
        String methodName = point.getSignature().getName();
        String class_method=simpleName+"-"+methodName;
        option.setOpClassMethod(class_method);
        //获取切入点方法的参数
        StringBuilder builder = new StringBuilder();
        Object[] args = point.getArgs();
        for (Object arg : args) {
            builder.append(arg);
            builder.append(",");
        }

        String arg_str = builder.toString();
        option.setOpParams(arg_str);

        Object proceed = point.proceed();
        option.setOpReturnValue(proceed+"");
        service.insert(option);
        return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }
    }
}
